package com.gy.yemall.yemallelasticsearch.controller;

import com.gy.common.exception.CodeException;
import com.gy.common.to.es.SkuEsModel;
import com.gy.common.utils.R;
import com.gy.yemall.yemallelasticsearch.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("yemallelasticsearch/save")
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (Exception e) {
            log.error("商品保存异常");
            return R.error(CodeException.PRODUCT_SAVE_EXCEPTION.getCode() , CodeException.PRODUCT_SAVE_EXCEPTION.getMsg());
        }

        if (!b) {
            return R.ok();
        } else {
            log.error("商品保存失败");
            return R.error(CodeException.PRODUCT_SAVE_EXCEPTION.getCode() , CodeException.PRODUCT_SAVE_EXCEPTION.getMsg());
        }

    }

}
